//
//  AXHomeZjzdDetailsVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/29.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeZjzdDetailsVC.h"
#import "Header.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXHomeZjzdIntroCell.h"
#import "AXHomeZjzdEvaluateCell.h"
#import "AXHomeZjzdEvaluateModel.h"
#import "UIViewController+HBD.h"
#import "UIImageView+WebCache.h"
#import "AXHomeZjzdIntroModel.h"
#import "AXHomeZjzdEvaluateVC.h"


#define STATUSBAR_HEIGHT ([[UIApplication sharedApplication] statusBarFrame].size.height)
#define NAVBAR_HEIGHT (STATUSBAR_HEIGHT+44)

@interface AXHomeZjzdDetailsVC ()<UITableViewDelegate,UITableViewDataSource>



@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic , strong) NSMutableArray<AXHomeZjzdIntroModel *> *modelsArray1;
/** 用户评论 */
@property (nonatomic, strong) NSMutableArray<AXHomeZjzdEvaluateModel *>  *EvaluateModelsArray;

@property (nonatomic , strong) AXHomeZjzdIntroModel *introModel;

@end

@implementation AXHomeZjzdDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    [self setupNavigationItems];
    
   

    /** 导航条白色 */
    self.hbd_barTintColor = [UIColor whiteColor];
    self.hbd_barShadowHidden = YES;
    
    [self requestDetail];
    [self requestcommentList];
    
   
}


#pragma mark -action


//自定义UIBarButtonItem
- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 60, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    titleLabel.text = @"专家详情";
    self.navigationItem.titleView = titleLabel;
    
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)creatView {
    
  
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //注册cell
    [self.tableView registerClass:[AXHomeZjzdIntroCell class] forCellReuseIdentifier:[AXHomeZjzdIntroCell reuseID]];
    [self.tableView registerClass:[AXHomeZjzdEvaluateCell class] forCellReuseIdentifier:[AXHomeZjzdEvaluateCell reuseID]];
    
 
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(Wi)
    .heightIs(SCREEN_HEIGHT-NAVIGATION_HEIGHT);
    
    
}






- (void)requestDetail {
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"fExpertId"] = self.fExpertId;
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_selectExpertDetilByUserId) success:^(id respose) {
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
           
            self.introModel = [AXHomeZjzdIntroModel mj_objectWithKeyValues:respose[@"body"][@"data"]];
            
             [weakSelf.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

/** 评论列表 */

- (void)requestcommentList {
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"fExpertId"] = self.fExpertId;
 
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_selectExpertEvaluatePage) success:^(id respose){
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
           
            weakSelf.EvaluateModelsArray = [AXHomeZjzdEvaluateModel  mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"][@"list"]];
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        
       
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    }];
    
    
}





#pragma mark - tableViewDelegate & tableViewDatasorce



//分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 0;
    }
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    
    UIView *topLine = [[UIView alloc]init];
    topLine.backgroundColor = COLOR_LINE;
    [view addSubview:topLine];
    
    topLine.sd_layout
    .leftSpaceToView(view, 0)
    .topSpaceToView(view, 0)
    .widthIs(SCREEN_WIDTH)
    .heightIs(10);
    
    
    UILabel *commentLabel = [[UILabel alloc]init];
    commentLabel.text = @"用户评价";
    commentLabel.textColor = COLOR_DIV;
    commentLabel.font = FONT_FOURTEEN;
    [view addSubview:commentLabel];
    
    commentLabel.sd_layout
    .leftSpaceToView(view, 10)
    .topSpaceToView(topLine, 0)
    .widthIs(60)
    .heightIs(44);
    
    /**  查看按钮 */
    UIImageView *img = [[UIImageView alloc]init];
    img.image = [UIImage imageNamed:@"grayBack"];
    [view addSubview:img];
    
    img.sd_layout
    .rightSpaceToView(view, 10)
    .centerYEqualToView(view)
    .widthIs(15)
    .heightIs(15);
    
    UILabel *check = [[UILabel alloc]init];
    check.textAlignment = NSTextAlignmentRight;
    check.text = @"查看所有评价";
    check.userInteractionEnabled = YES;
    check.textColor = COLOR_ORG;
    check.font = FONT_FOURTEEN;
    [view addSubview:check];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action:)];
    [check addGestureRecognizer:tap];
    
    check.sd_layout
    .rightSpaceToView(img, 10)
    .topEqualToView(commentLabel)
    .leftSpaceToView(commentLabel, 10)
    .heightIs(44);
    
    
    UIView *botLine = [[UIView alloc]init];
    botLine.backgroundColor = COLOR_LINE;
    [view addSubview:botLine];
    
    botLine.sd_layout
    .leftSpaceToView(view, 0)
    .bottomSpaceToView(view, 0)
    .widthIs(SCREEN_WIDTH)
    .heightIs(10);
    
    
    
    
    return view;
    
    
}
/** 分区的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
    }
    return 64;
    
}

//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
      
        return 1;
    }
    
    return 3;
}

/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   

    if (indexPath.section == 1) {
        

            
              return [tableView cellHeightForIndexPath:indexPath model:self.EvaluateModelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHomeZjzdEvaluateCell class] contentViewWidth:SCREEN_WIDTH];
        
       
       
    }
    
     return 250;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        AXHomeZjzdIntroCell *cell1 = [tableView dequeueReusableCellWithIdentifier:[AXHomeZjzdIntroCell reuseID] forIndexPath:indexPath];
        [cell1 setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell1.model = self.introModel;
       
        /** 专家图像 */
         [cell1.specialistImg sd_setImageWithURL:[NSURL URLWithString:self.fProfilePhoto] placeholderImage:[UIImage imageNamed:@""]];
        return cell1;
    }
    
    AXHomeZjzdEvaluateCell *cell2 = [tableView dequeueReusableCellWithIdentifier:[AXHomeZjzdEvaluateCell reuseID] forIndexPath:indexPath];
    [cell2 setSelectionStyle:UITableViewCellSelectionStyleNone];
    
   
       
    cell2.model = self.EvaluateModelsArray[indexPath.row];
    
  
    
    
    
    return cell2;
    
    
}





- (NSMutableArray<AXHomeZjzdIntroModel *> *)modelsArray1 {
    
    if (!_modelsArray1) {
        _modelsArray1 = [NSMutableArray<AXHomeZjzdIntroModel *> array];
    }
    return _modelsArray1;
    
}

- (NSMutableArray<AXHomeZjzdEvaluateModel *> *)modelsArray {
    
    if (!_EvaluateModelsArray) {
        
        _EvaluateModelsArray = [NSMutableArray<AXHomeZjzdEvaluateModel *> array];
    }
    return _EvaluateModelsArray;
}



#pragma mark - action

- (void)action:(UIButton *)btn {
    
    AXHomeZjzdEvaluateVC *vc = [[AXHomeZjzdEvaluateVC alloc]init];
    vc.fExpertId = self.fExpertId;
    [self.navigationController pushViewController:vc animated:YES];
    
}



@end

